package com.hemaapp.yjnh.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.CmnBookMerchantListActivity;
import com.hemaapp.yjnh.activity.FarmerListActivity;
import com.hemaapp.yjnh.bean.HotDistrict;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.ImageUtils;

/**村游多热门区域,以及田家中的热门区域
 * @author GuiyouZhang
 * Date：2015年10月8日 下午3:29:32
 * Email:guiyouzhang@163.com
 */
public class HotAreaAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<HotDistrict> hotDistricts = new ArrayList<HotDistrict>();
	private boolean showEmpty = true;//当无数据时是否显示提示
	private int type = 1;//1村游热门，2田家热门, 3村游省份下的小热门
	
	public HotAreaAdapter(Context context, ArrayList<HotDistrict> areas, int type) {
		super(context);
		mContext = context;
		hotDistricts = areas;
		this.type = type;
	}
	
	public void setShowEmpty(boolean showEmpty) {
        this.showEmpty = showEmpty;
    }

    @Override
	public int getCount() {
        int size = hotDistricts.size();
        if (size == 0) {
            return showEmpty?1:0;
        }else {
            return size;
        }
	}

	@Override
    public boolean isEmpty() {
        int size = hotDistricts == null? 0:hotDistricts.size();
        return size == 0;
    }
	
	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty())
            return getEmptyView(parent);
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			if (type == 3) {
				convertView = LayoutInflater.from(mContext).inflate(
	                    R.layout.layout_hot_district2, null);
			}else {
				convertView = LayoutInflater.from(mContext).inflate(
	                    R.layout.layout_hot_district1, null);
			}
			itemFindView(convertView, holder);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		setItem(position, convertView, holder);
		return convertView;
	}

	private class ViewHolder {
		private ImageView backImage;
		private TextView tv_district;
	}
	
	private void itemFindView(View view, ViewHolder ivHolder) {
		ivHolder.backImage = (ImageView) view.findViewById(R.id.iv_goods);
		ivHolder.tv_district = (TextView) view.findViewById(R.id.tv_district);
	}
	
	private void setItem(int position, View view, ViewHolder ivHolder) {
		final HotDistrict district = hotDistricts.get(position);
		ImageUtils.loadBigImage(mContext, district.getimgurlbig(), ivHolder.backImage);
		ivHolder.tv_district.setText(district.getdistrict_name());
		view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					String token = "";
					User user = BaseApplication.getInstance().getUser();
					if (user != null) {
						token = user.getToken();
					}
					//1村游热门，2田家热门, 3村游省份下的小热门
					((BaseActivity)mContext).getNetWorker().ADRecord(token, "4", district.getId());
					String[] areas = district.getdistrict_name().split("—");
					Intent itDistrict = new Intent();
					if (type == 1 || type == 3) {
						itDistrict.setClass(mContext, CmnBookMerchantListActivity.class);
						itDistrict.putExtra("keytype", "4");
						itDistrict.putExtra("keyid", areas[areas.length-1]);
						itDistrict.putExtra("role", "");
						itDistrict.putExtra("title", areas[areas.length-1]);
					}else if (type == 2) {
						itDistrict.setClass(mContext, FarmerListActivity.class);
						itDistrict.putExtra("keyid", areas[areas.length - 1]);
						itDistrict.putExtra("title", areas[areas.length - 1]);
						itDistrict.putExtra("change", false);
					}
					mContext.startActivity(itDistrict);
				} catch (Exception e) {
					Toast.makeText(mContext, "地区数据异常\n请稍后再试", Toast.LENGTH_LONG).show();
				}
			}
		});
	}
}
